package com.ieding.www.action.user;

import java.io.IOException;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ieding.www.bean.ContactInfo;
import com.ieding.www.bean.Employee;

public class UserAction extends HttpServlet {

	public void init(ServletConfig config) throws ServletException {
		Employee employee = new Employee("Alfred1", "Smith", new Date());
		Employee employee2 = new Employee("Alfred2", "Smith2", new Date());

		ContactInfo contactInfo = new ContactInfo();
		contactInfo.setStreetAddress("aaaaa");

		PersistenceManager pm = PMF.get().getPersistenceManager();

		try {
			// pm.makePersistent(contactInfo);
			employee.setContactInfo(contactInfo);
			pm.makePersistent(employee);
			pm.makePersistent(employee2);

		} finally {
			pm.close();
		}
	}

	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String query = "select from " + Employee.class.getName() + "";
		PersistenceManager pm = PMF.get().getPersistenceManager();
		List<Employee> employees = (List<Employee>) pm.newQuery(query)
				.execute();

		for (Iterator iterator = employees.iterator(); iterator.hasNext();) {
			Employee employee2 = (Employee) iterator.next();
			System.out.println(":::::::::::::" + employee2.getFirstName());
			System.out.println(employee2.getId());
			if (employee2.getContactInfo() != null) {
				System.out.println(employee2.getContactInfo()
						.getStreetAddress());
				System.out.println(employee2.getContactInfo()
						.getKey());
			}else{
//				pm.deletePersistent(employee2);
			}
		}
		// String query = "select from " + ContactInfo.class.getName()
		// + " where streetAddress == 'aaaaa'";
		// PersistenceManager pm = PMF.get().getPersistenceManager();
		// List<ContactInfo> ContactInfos = (List<ContactInfo>)
		// pm.newQuery(query)
		// .execute();
		//
		// for (Iterator iterator = ContactInfos.iterator();
		// iterator.hasNext();) {
		// ContactInfo contactInfo = (ContactInfo) iterator.next();
		// System.out.println(":::::::::::::" + contactInfo.getStreetAddress());
		// System.out.println(contactInfo.getKey());
		// }

	}
}
